package dict

import (
	"context"
	"fmt"
	model "service/model/crm_base"
	"service/model/mod"
	"time"

	"gitee.com/cloudapex/ulern/util"

	com "gitee.com/cloudapex/protos/comm/pb"
	gpb "gitee.com/cloudapex/protos/crm_base/pb"
	gpp "gitee.com/cloudapex/protos/crm_order/pb"
	"google.golang.org/grpc"

	"gitee.com/cloudapex/ulern/htp"
	"github.com/gin-gonic/gin"
)

var cc *grpc.ClientConn

/*
 * 描述: 项目列表
 * 路由: /dict/projectList
 * 方法: GET
 *********************************************************************/
type ProjectList struct {
	Id    int64  `form:"id" binding:"omitempty"`    // -- ID (新PID)
	ProId int64  `form:"proId" binding:"omitempty"` //	-- 项目ID(老PID)
	Name  string `form:"name" binding:"omitempty"`  //	-- 项目名称
	CId   int64  `form:"cid" binding:"omitempty"`   //	-- 公司ID
	State int64  `form:"state" binding:"omitempty"` //  -- 状态 默认1，启用 2.关闭
}

//项目列表返回参数
type ProjectListRes struct {
	ID    int64  `json:"id"`     //ID
	PId   int64  `json:"p_id"`   //项目ID
	PName string `json:"p_name"` //项目名称
}

func (this *ProjectList) Handle(c *gin.Context, ctx context.Context) htp.Response {
	jwtUser := mod.CurrUser(c)
	var project gpb.Project
	project.Id = this.Id
	project.ProId = this.ProId
	project.Name = this.Name
	project.CId = jwtUser.CId
	project.State = this.State
	sysreq := gpb.ProjectListReq{
		Project: &project,
		Page:    1,
		Count:   100,
		DesClum: 1,
		IsDes:   2,
	}
	res, err := model.Proj.GetProjectList(ctx, &sysreq)
	fmt.Println(err, "-----------------------------------")
	fmt.Println(res, "=++++++++++++++++++++++++===========")
	if err != nil {
		return htp.RespConvertErr("", err)
	}
	val := make([]ProjectListRes, 0)
	for _, v := range res.Project {
		val = append(val, ProjectListRes{v.Id, v.ProId, v.Name})
	}
	return htp.RespOK("", &struct {
		List interface{} `json:"list"`
	}{val})
}

/*
 * 描述: 支付类型
 * 路由: /dict/payTypes
 * 方法: GET
 *********************************************************************/
type PayTypes struct{}

func (this *PayTypes) Handle(c *gin.Context, ctx context.Context) htp.Response {
	// user := mod.CurrUser(c)

	return htp.RespOK("", &struct {
		List interface{} `json:"list"`
	}{mod.PayTypes})
}

/*
 * 描述: 订单类型
 * 路由: /dict/orderTypes
 * 方法: GET
 *********************************************************************/
type OrderTypes struct{}

func (this *OrderTypes) Handle(c *gin.Context, ctx context.Context) htp.Response {
	// user := mod.CurrUser(c)

	return htp.RespOK("", &struct {
		List interface{} `json:"list"`
	}{mod.OrderTypes})
}

/*
 * 描述: 学校列表
 * 路由: /dict/school
 * 方法: GET
 *********************************************************************/
type SchoolList struct {
	Page  int64 `form:"page" json:"page" binding:"omitempty"`   // 页码
	Pnum  int64 `form:"pnum" json:"pnum" binding:"omitempty"`   // 单页数量
	Total int64 `form:"total" json:"total" binding:"omitempty"` // 总数
	Order int64 `form:"order" json:"order" binding:"omitempty"` // 排序字段(默认空,则程序用默认字段)
	Orule int64 `form:"orule" json:"orule" binding:"omitempty"` // 1:倒叙; 2:升序;
}

func (this *SchoolList) Handle(c *gin.Context, ctx context.Context) htp.Response {
	school := gpp.SchoolCreateReq{Pager: &com.PageResponse{
		Count: 200,
		Page:  1,
		Order: 1,
		Orule: 1,
	}}
	rep, err := model.COmmS.SchoolList(ctx, &school)
	if err != nil {
		return htp.RespModelErr("", err)
	}
	return htp.RespOK("", &struct {
		Total int64       `json:"total"`
		List  interface{} `json:"list"`
	}{rep.Pager.Total, rep.Data})
}

/*
 * 描述: 协助报名
 * 路由: /dict/helpsign
 * 方法: GET
 *********************************************************************/
type HelpSignList struct {
	Page  int64 `form:"page" json:"page" binding:"omitempty"`   // 页码
	Pnum  int64 `form:"pnum" json:"pnum" binding:"omitempty"`   // 单页数量
	Total int64 `form:"total" json:"total" binding:"omitempty"` // 总数
	Order int64 `form:"order" json:"order" binding:"omitempty"` // 排序字段(默认空,则程序用默认字段)
	Orule int64 `form:"orule" json:"orule" binding:"omitempty"` // 1:倒叙; 2:升序;
}

type HelpSingeItme struct {
	HelpId    int64  `protobuf:"varint,1,opt,name=helpId,proto3" json:"helpId,omitempty"`       // 报名ID
	ProjectId int64  `protobuf:"varint,2,opt,name=projectId,proto3" json:"projectId,omitempty"` // 项目ID
	PName     string `protobuf:"bytes,3,opt,name=pName,proto3" json:"pName,omitempty"`          // 项目名称
	UserId    int64  `protobuf:"varint,4,opt,name=userId,proto3" json:"userId,omitempty"`       // 创建着ID
	Price     int64  `protobuf:"varint,5,opt,name=price,proto3" json:"price,omitempty"`         // 价格
	Status    int64  `protobuf:"varint,6,opt,name=status,proto3" json:"status,omitempty"`       // 状态
	CreateAt  int64  `protobuf:"varint,7,opt,name=createAt,proto3" json:"createAt,omitempty"`   // 创建时间
}

func (this *HelpSignList) Handle(c *gin.Context, ctx context.Context) htp.Response {
	var project gpb.Project
	rep, err := model.COmmS.HelpSignList(ctx, &gpp.HelpSignListReq{
		Pager: &com.PageResponse{
			Count: 100,
			Page:  1,
			Order: 1,
			Orule: 1,
		},
	})
	if err != nil {
		return htp.RespModelErr("", err)
	}
	pidlist := gpb.ProjectListReq{Project: &project, Page: 1, Count: 100, DesClum: 1, IsDes: 2}
	projct, errs := model.Proj.GetProjectList(ctx, &pidlist)
	if errs != nil {
		return htp.RespModelErr("", errs)
	}
	val := make([]ProjectListRes, 0)
	for _, v := range projct.Project {
		val = append(val, ProjectListRes{v.Id, v.ProId, v.Name})
	}
	userlist := make([]HelpSingeItme, 0)
	for _, s := range rep.Data {
		var info HelpSingeItme
		util.Convert(&info, s)
		for _, i := range val {
			if i.PId == s.ProjectId {
				info.PName = i.PName
			}
		}
		userlist = append(userlist, info)
	}
	return htp.RespOK("", &struct {
		Total int64       `json:"total"`
		List  interface{} `json:"list"`
	}{rep.Pager.Total, userlist})
}

/*
 * 描述: 证书
 * 路由: /dict/cert
 * 方法: GET
 *********************************************************************/
type CertList struct {
	Page  int64 `form:"page" json:"page" binding:"omitempty"`   // 页码
	Pnum  int64 `form:"pnum" json:"pnum" binding:"omitempty"`   // 单页数量
	Total int64 `form:"total" json:"total" binding:"omitempty"` // 总数
	Order int64 `form:"order" json:"order" binding:"omitempty"` // 排序字段(默认空,则程序用默认字段)
	Orule int64 `form:"orule" json:"orule" binding:"omitempty"` // 1:倒叙; 2:升序;
}

func (this *CertList) Handle(c *gin.Context, ctx context.Context) htp.Response {
	// user := mod.CurrUser(c)
	rep, err := model.COmmS.CertList(ctx, &gpp.CertListReq{
		Pager: &com.PageResponse{
			Count: 100,
			Page:  1,
			Order: 1,
			Orule: 1,
		},
	})
	if err != nil {
		return htp.RespModelErr("", err)
	}
	return htp.RespOK("", &struct {
		Total int64       `json:"total"`
		List  interface{} `json:"list"`
	}{rep.Pager.Total, rep.Data})
}

/*
 * 描述: 推广渠道列表
 * 路由: /dict/tgChannels
 * 方法: GET
 *********************************************************************/
type TgChannels struct {
	State int32 `form:"state" json:"state" binding:"omitempty"` // 状态 0 正常 1 停用
}
type tgChannelsRspItem struct {
	Id   int64  `json:"id"`   // -- 唯一ID
	Name string `json:"name"` // -- 推广渠道名
}

func (this *TgChannels) Handle(c *gin.Context, ctx context.Context) htp.Response {
	//user := mod.CurrUser(c)
	rsp, err := model.Channel.GetChannelList(ctx, &gpb.ChanLiReq{
		Page:    1,
		Count:   1000,
		DesClum: 1,
		IsDes:   2,
		Channel: &gpb.Channel{Start: int64(this.State)},
	})
	if err != nil {
		return htp.RespModelErr("", err)
	}

	items := []tgChannelsRspItem{}
	if err := util.Convert(&items, rsp.Channel); err != nil {
		return htp.RespConvertErr("", err)
	}

	return htp.RespOK("", &struct {
		List interface{} `json:"list"`
	}{items})
}

/*
 * 描述: 推广方式列表
 * 路由: /dict/tgWays
 * 方法: GET
 *********************************************************************/
type TgWays struct {
	State int32 `form:"state" json:"state" binding:"omitempty"` // 状态 0 正常 1 停用
}
type tgWaysRspItem struct {
	Id   int64  `json:"id"`   // -- 唯一ID
	Name string `json:"name"` // -- 推广方式名
}

func (this *TgWays) Handle(c *gin.Context, ctx context.Context) htp.Response {
	//user := mod.CurrUser(c)
	rsp, err := model.Channel.GetProWayList(ctx, &gpb.WayLiReq{
		Page:    1,
		Count:   1000,
		DesClum: 1,
		IsDes:   2,
		ProWay:  &gpb.ProWay{Start: int64(this.State)},
	})
	if err != nil {
		return htp.RespModelErr("", err)
	}

	items := []tgWaysRspItem{}
	if err := util.Convert(&items, rsp.ProWay); err != nil {
		return htp.RespConvertErr("", err)
	}

	return htp.RespOK("", &struct {
		List interface{} `json:"list"`
	}{items})
}

//获取网络当前时间
type TimeItem struct {
}

func (this *TimeItem) Handle(c *gin.Context, ctx context.Context) htp.Response {
	at := time.Now().Unix()
	return htp.RespOK("", &struct {
		Time int64 `json:"time"` // 获取当前网课时间
	}{at})
}
